<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'






import { userInfoService } from '@/api/user.js'
import useUserInfoStore from '@/stores/userinfo.js'//写了默认导出 不需加括号
import { useTokenStore } from '@/stores/token.js'
const tokenStore = useTokenStore();
const userInfoStore = useUserInfoStore();
//调用函数获取用户详细信息展示在顶部导航栏
const getUserInfo = async()=>{
     let result = await userInfoService();
   //数据存储到pinia中
   userInfoStore.setInfo(result.data)
}
getUserInfo();

import { useRouter } from 'vue-router';
import { ElMessage,ElMessageBox } from 'element-plus';
//得到路由器
const router = useRouter()
//条目被点击后调用的函数
const handleCommand = (command)=>{
    //判断指令
    if(command=== 'logout'){
        //退出登录
        ElMessageBox.confirm(
    '确认退出吗?',
    '提示',
    {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
    }
    )
    .then(async() => {
        //退出登录
        //pinia保存着个人的token
        //1.清空pinia中存储的token和个人信息
        tokenStore.removeToken()
        userInfoStore.removeInfo()
        //2.跳转到登录页面
        router.push('/login')
    ElMessage({
        type: 'success',
        message: '退出登录成功',
    })
    })
    .catch(() => {
    ElMessage({
        type: 'info',
        message: '取消退出登录',
    })
    })
    }else{
        //路由
        router.push('/user/'+command)
    }

}
</script>

<template>
    <!--element-plus的容器-->
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <!--element-plus的菜单标签 el-menu-item菜单项 el-sub-menu子菜单-->
            <el-menu active-text-color="#ffd04b" background-color="#232323"  text-color="#fff"
                router>
                <!-- 子路由 -->
                  <el-menu-item index="/user/home">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>首页</span>
                </el-menu-item>
                <el-menu-item index="/article/family">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>家庭补助</span>
                </el-menu-item>
                <el-menu-item index="/article/loan">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>学生贷款</span>
                </el-menu-item>
                <el-menu-item index="/article/baby">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>育儿补贴</span>
                </el-menu-item>
                <el-menu-item index="/article/land">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>耕地补贴</span>
                </el-menu-item>
                <el-sub-menu >
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/user/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                   
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 数据渲染到界面-->
            <el-header>
                <div>补助系统:<strong>{{userInfoStore.info.name}}</strong></div>
                <!--下拉菜单-->
                <!--command事件：条目被点击后会触发@command 在事件函数上可以声明一个参数handleCommand接收条目对应的指令-->
                <!--点击基本资料时command后面的info就是传递给这个事件函数的指令-->
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <!-- <div style="width: 1290px; height: 570px;border: 1px solid red;">
                    内容展示区
                </div> -->
                <!-- 子路由 -->
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>补贴资金查询与公示平台</el-footer>
        </el-container>
    </el-container>
</template> 

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  overflow: hidden;
  background-color: #f5f7fa;
}

.el-aside {
  background-color: #1d2129;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 2px 0 12px rgba(0, 0, 0, 0.15);
  }

  &__logo {
    height: 120px;
    background: url('@/assets/background.jpg') no-repeat center / 210px auto;
    position: relative;
    overflow: hidden;
    
    // 增加logo底部装饰线
    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 15%;
      width: 70%;
      height: 2px;
      background: linear-gradient(90deg, transparent, #409eff, transparent);
    }
  }

  .el-menu {
    border-right: none;
    background-color: transparent;
    padding-top: 15px;

    .el-menu-item {
      color: #c9cdD4;
      height: 50px;
      line-height: 50px;
      margin: 8px 10px;
      border-radius: 6px;
      padding-left: 25px !important;
      transition: all 0.2s ease;

      &:hover {
        background-color: rgba(64, 158, 255, 0.1);
        color: #409eff;
      }

      &.is-active {
        background-color: #409eff;
        color: #fff;
        box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
      }

      .el-icon {
        margin-right: 12px;
        font-size: 18px;
      }
    }
  }
}

.el-header {
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 60px !important;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  z-index: 10;

  .el-dropdown__box {
    display: flex;
    align-items: center;
    padding: 5px 10px;
    border-radius: 20px;
    transition: background-color 0.2s;

    &:hover {
      background-color: #f5f7fa;
    }

    .el-icon {
      color: #666;
      margin-left: 10px;
      font-size: 18px;
      cursor: pointer;
      transition: color 0.2s;

      &:hover {
        color: #409eff;
      }
    }

    &:active,
    &:focus {
      outline: none;
    }
  }
}

.el-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #86909c;
  background-color: #f5f7fa;
  border-top: 1px solid #eee;
  height: 50px !important;
}

// 主内容区样式
.el-main {
  padding: 20px;
  overflow-y: auto;
  background-color: #f5f7fa;
}
</style>